package ru.spb.bragnikita.myevents.ibatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @author Nikita.Brazhnikov
 */
public class IBatisUtils {

	private static IBatisUtils instance;
	private SqlSessionFactory factory;

	private IBatisUtils() {
		try {
			String resource = "Configuration.xml";
			Reader reader = Resources.getResourceAsReader(resource);
			factory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public synchronized static SqlSessionFactory getFactory() {
		if (instance == null)
			instance = new IBatisUtils();
		return instance.factory;
	}

}
